<?php
    require_once ('classes/user.class.php');
    require_once('classes/mysql.php');
    require_once ('classes/toolbox.class.php');
    
    if(session_id()=="") session_start ();
    
    if(isset($_SESSION['user']) && $_SESSION['user']->isAdmin()){
?>
    <script type="text/javascript" src="ckeditor/ckeditor.js"></script>
    <script type="text/javascript" src="ckeditor/adapters/jquery.js"></script>
    <script type="text/javascript" src="js/manage_functions.js"></script>
<?php
        
        if(!isset($_POST['group'])){
            
?>


<h2>Envoi d'un message groupé</h2>

<div id="message_group_content">
    <form id="group_message_form" method="POST" action="index.php?page=manage&manage=message_group">
        Sélectionner le groupe d'utilisateurs destinataire du message :
        <SELECT id="user_select" name="group">
            <option value="everybody">Tout le monde</option>
            <option value="members">Membres du club</option>
            <option value="admins">Administrateurs</option>
        </select>
        <br/>
        <br/>
        <label  for="subject">Objet du message:</label>
        <input id="message_subject" name="subject "type="text" class="field text large"/>
        <br/>
        <br/>
        <br/>
        <br/>
        <textarea id="editor_textarea"></textarea>
        <br/>
        <br/>
        <button type="submit" class="positive button">Envoyer</button>
    </form>
</div>

<script type="text/javascript" >
       //transforme la textarea en objet JS ckeditor
       $( '#editor_textarea' ).ckeditor();
       $('#group_message_form').submit(function(event){
            event.preventDefault();
            if(check_group_message())
                this.submit();
        })
</script>

<?php 
        }else{
            $toolbox = new ToolBox();
            
            $recipients = array();
            $group = $_POST['group'];
            $subject = $_POST['message_subject'];
            $body = $toolbox->trimUltime($_POST['editor_textarea']);
            
            $bdd = new MySQL();
            
            switch($group){
                case 'everybody':
                    $query = "SELECT * FROM user";
                    break;
                case 'members':
                    $query = 'SELECT *
                      FROM user,admin
                      WHERE user.id=admin.id
                      AND status=\'Membre\'';
                    break;
                case 'admins':
                    $query = 'SELECT *
                      FROM user,admin
                      WHERE user.id=admin.id
                      AND status=\'Administrateur\'';
            }
            
            if(!$result = $bdd->Send_Query($query))
                    die($bdd -> return_error());
            else{
                while ($row = $bdd->get_array($query)){
                    $recipients[]=$row['mail'];
                }
            }
            
            for($i=0;$i<count($recipients);$i++){
                //mail($recipients[i], $subject, $body);
                echo $recipients[i];
            }
            
            echo 'ok';
        }
    } 
?>

